package collection.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class ListTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        list.add("qianqi");
        list.add("zhangsan");
        System.out.println("list = " + list);
        list.set(2,"zhaoliu");
        System.out.println("list = " + list);
        System.out.println("list.get(1) = " + list.get(1));
        list.remove(2);
        System.out.println("list = " + list);
        System.out.println("list.indexOf(\"zhangsan\") = " + list.indexOf("zhangsan"));
        System.out.println("list.lastIndexOf(\"zhangsan\") = " + list.lastIndexOf("zhangsan"));
        List<String> strings = list.subList(0, 2);
        System.out.println("strings = " + strings);
        System.out.println("==================");
        ListIterator<String> it = list.listIterator();
        while (it.hasNext()) {
            String next = it.next();
            if ("lisi".equals(next)) {
                it.remove();
                System.out.println("it.nextIndex() = " + it.nextIndex());
                System.out.println("it.previousIndex() = " + it.previousIndex());
            }
            if ("qianqi".equals(next)) {
                it.add("niuma");
                it.previous();
            }
            if ("zhaoliu".equals(next)) {
                it.set("xiugai");
            }
            System.out.println("next = " + next);
        }
        System.out.println("=================================");
        for (String s : list) {
            System.out.println("s = " + s);
        }



    }
}
